/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package message;

import SingleLinkedList.SingleNode;
import folders2.ProjectFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author Administrator
 */
public class MessageFolder implements MessageFolderInterface
{
    private ProjectFile messFolder;
    private Message sms;
    private ProjectFile attachFolder;
    private Attachment attach;
    private ProjectFile text;
    private ProjectFile messInfo;      ///////////////////
    /**
     * This Constructor is used to create new message folder
     * and create its contents(i.e files such that text , messInfo , attachment folder)
     * text which contains message text part
     * messInfo which contains message information
     * attachment folder which contains all attachments
     * @param parent parent Folder (almost it is whether temp folder or user draft folder)
     * parent is temp folder when you sent this message
     * parent is user draft folder folder when you cancel sending
     * @throws java.io.IOException
     */
    public MessageFolder(File parent)throws IOException
    {
        Calendar now = Calendar.getInstance();
        messFolder = new ProjectFile(parent,now.getTimeInMillis()+"");
        messFolder.getMyFile().mkdir();
        sms = new Message();
        attachFolder = new ProjectFile(messFolder.getMyFile(),"attachmentFOlder");
        attachFolder.getMyFile().mkdir();
        attach = new Attachment();
        text = new ProjectFile(messFolder.getMyFile(),"Text");
        text.getMyFile().createNewFile();
        messInfo = new ProjectFile(messFolder.getMyFile() , "messInfo");
        messInfo.getMyFile().createNewFile();
    }
    /**
     * This Constructor is used to create new empty message folder
     * which is used when message folder data will be set after that
     */
    public MessageFolder()
    {
        messFolder = null;
        sms = null;
        attachFolder = null;
        attach = new Attachment();
        text = null;
        messInfo = null;
    }
    /**
     * This method is used to get reference of attachment folder
     * @return reference to attachment folder
     */
    public ProjectFile getAttachFolder()
    {
        return attachFolder;
    }
    /**
     * This method is set reference of attachment folder
     * @param attachFolder new reference of attachment folder
     */
    public void setAttachFolder(ProjectFile attachFolder)
    {
        this.attachFolder = attachFolder;
    }
    /**
     * This method is used to get attachment list which contains pathes
     * @return singleList of pathes
     */
    public Attachment getAttach()
    {
        return attach;
    }
    /**
     * This method is used to set attachment list which contains pathes
     * @param attach new attachment pathes
     */
    public void setAttach(Attachment attach)
    {
        this.attach = attach;
    }
    /**
     * This method is used to get reference of message folder
     * @return reference to message folder which contains all data
     */
    public ProjectFile getMessFolder()
    {
        return messFolder;
    }
    /**
     * This method is used to set reference of message folder
     * @param messFolder new reference of message folder
     */
    public void setMessFolder(ProjectFile messFolder)
    {
        this.messFolder = messFolder;
    }
    /**
     * This method is used to get reference of messInfo file which contains all message data
     * @return reference to messInfo
     */
    public ProjectFile getMessInfo()
    {
        return messInfo;
    }
    /**
     * This method is used to set reference of messInfo file which contains all message data
     * @param messInfo new reference of messInfo
     */
    public void setMessInfo(ProjectFile messInfo)
    {
        this.messInfo = messInfo;
    }
    /**
     * This method is used to get message object
     * @return message object
     */
    public Message getSms()
    {
        return sms;
    }
    /**
     * This method is used to set message object
     * @param sms new message object
     */
    public void setSms(Message sms)
    {
        this.sms = sms;
    }
    /**
     * This method is used to get reference of message text file
     * @return reference of message text file
     */
    public ProjectFile getText()
    {
        return text;
    }
    /**
     * This method is used to set reference of message text file
     * @param text new reference of text file
     */
    public void setText(ProjectFile text)
    {
        this.text = text;
    }
    /**
     * This method is used to add attachment to message folder
     * @param path
     */
    public void addAttachment(String path)
    {
        attach.addPath(path);
    }
    /**
     * This method is used to copy all attachment from pathes in attachment list
     * to message attachment folder
     * @throws java.io.IOException
     */
    public void moveTo() throws IOException
    {
        int i = 0;
        SingleNode u = attach.getPathes().getHead();
        while(i < attach.getPathes().getSize())
        {
            File f = new File((String)u.getData());
            attachFolder.copy(f);
            u = u.getNext();
            i++;
        }
    }
    /**
     * This method is used to read message text from message text file
     * @return result of type String which contains all text in formation
     * @throws java.io.IOException
     */
    public String readTextFile()throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(this.getText().getMyFile()));
        String line = "";
        String data = "";
        while((line = in.readLine())!= null)
        {
            data +=line+"\n";
        }
        return data;
    }
    /**
     * This method is used to write text file
     * @param words message text
     * @throws java.io.IOException
     */
    public void writeFileText(String words) throws IOException
    {
        BufferedWriter out = new BufferedWriter(new FileWriter(text.getMyFile()));
        out.write(words);
        out.close();
    }
    /**
     * This method is used to write message information
     * @throws java.io.IOException
     */
    public void writeMessageInfo() throws IOException
    {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-mm-dd");
        BufferedWriter out = new BufferedWriter(new FileWriter(messInfo.getMyFile()));
        String data = sms.getName()+","+sms.getSubject()+","+sms.getReceiver()+","+sms.getSender()+","+f.format(sms.getSendDate())+","+sms.isRead();
        out.write(data);
        out.close();
    }
    /**
     * This method is used to find message components in agiven folder
     * and set message data(this method used when user want to read his messages )
     * @param u folder which contains message data (message data....>> files && attachment folder)
     * @throws java.io.IOException
     */
    public void findMessageComponent(File u)throws IOException
    {
        messFolder = new ProjectFile(u);
        JFileChooser chooser = new JFileChooser();
        FileSystemView v = chooser.getFileSystemView();
        text = new ProjectFile (v.getChild(messFolder.getMyFile(), "Text"));
        attachFolder = new ProjectFile(v.getChild(messFolder.getMyFile(), "attachmentFOlder"));
        File f = v.getChild(messFolder.getMyFile(), "messInfo");
        messInfo = new ProjectFile(f);
        sms = new Message();
        sms.readMessageInfo(messInfo.getMyFile());
        /*sms.readMessageInfo(v.getChild(messFolder.getMyFile(), "messInfo"));*/
    }
}
